package com.neu.cloudFactory.service;

import com.neu.cloudFactory.DAO.*;
import com.neu.cloudFactory.PO.Bid;
import com.neu.cloudFactory.PO.Factory;
import com.neu.cloudFactory.PO.Order;
import com.neu.cloudFactory.VO.BidInfoVO;
import com.neu.cloudFactory.VO.OrderVO;
import org.aspectj.weaver.ast.Or;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class OrderService {
    final OrderDAO orderDAO;
    final BidDAO bidDAO;
    final ProductMsgDAO productMsgDAO;
    final UserDAO userDAO;
    final FactoryDAO factoryDAO;
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");


    public OrderService(OrderDAO orderDAO, BidDAO bidDAO, ProductMsgDAO productMsgDAO, UserDAO userDAO, FactoryDAO factoryDAO) {
        this.orderDAO = orderDAO;
        this.bidDAO = bidDAO;
        this.productMsgDAO = productMsgDAO;
        this.userDAO = userDAO;
        this.factoryDAO = factoryDAO;
    }

    //所有订单列表
    public List<OrderVO> findAllOrder() {
        List<Order> orderList = orderDAO.findAll();
        List<OrderVO> orderVOList = new ArrayList<>();
        for (Order order : orderList) {
            orderVOList.add(new OrderVO(order));
        }
        return orderVOList;
    }

    //工厂中的订单列表
    public List<OrderVO> findAllOrderForFactory() {
        List<Order> orderList = orderDAO.findAll();
        List<OrderVO> orderVOList = new ArrayList<>();
        for (Order order : orderList) {
            if (!(order.getOrderStateForCustomer().equals("已保存"))) {
                OrderVO orderVO = new OrderVO(order);
                orderVO.setFactoryAccount(userDAO.getById(factoryDAO.getById(order.getFactoryId()).getUserId()).getAccount());
                //TODO:for testing ,delete later
                System.out.println(orderVO);
                orderVOList.add(orderVO);
            }
        }
        return orderVOList;
    }

    //经销商中的订单列表
    public List<OrderVO> findAllOrderForCustomer(String account) {
        System.out.println(account);
        System.out.println(userDAO.findByAccount(account));
        System.out.println(userDAO.findByAccount(account).getId());
        List<Order> orderList = orderDAO.findByUserAccount(account);
        List<OrderVO> orderVOList = new ArrayList<>();
        for (Order order : orderList) {
            orderVOList.add(new OrderVO(order));
        }
        return orderVOList;
    }

    //修改经销商订单状态
    public void modifyCustomerState(Order order, String state) {
        orderDAO.modifyCustomerState(state, order.getId());
    }

    //修改云工厂订单状态
    public void modifyFactoryState(Order order, String state) {
        orderDAO.modifyFactoryState(state, order.getId());
    }

    //新增订单
    public void addOrder(Order order) {
        orderDAO.save(order);
    }

    //删除订单
    public void deleteOrder(Order order) {
        orderDAO.delete(order);
    }

    //投标详情
    public List<BidInfoVO> findAllBid(Order order) {
        List<Bid> bidList = bidDAO.findByOrderId(order.getId());
        List<BidInfoVO> bidInfoVOList = new ArrayList<>();
        //转化为VO
        for (Bid bid : bidList) {
            //找到投标工厂
            Factory factory = factoryDAO.findByUserId(userDAO.findByAccount(bid.getFactoryAccount()).getId());
            bidInfoVOList.add(new BidInfoVO( bid, userDAO.getById(factory.getUserId()),factory));
        }
        return bidInfoVOList;
    }

    //工厂进行投标
    public void addBid(Bid bid) {
        bidDAO.save(bid);
    }

//    经销商选标
    public void chooseBid(Bid bid) {
        System.out.println(bid);
        orderDAO.modifyCustomerState("投标结束", bid.getOrderId());
        orderDAO.modifyFactoryState("投标结束",bid.getOrderId());
        bidDAO.modifyState("已中标", bid.getId());
        //TODO:同一订单下的其他bid均未中，未中标工厂看到的状态应为未中标（不能排产）
        orderDAO.modifyFactoryId(
                factoryDAO.findByUserId(userDAO.findByAccount(bid.getFactoryAccount()).getId()).getId(), bid.getOrderId());
    }

    public Order changeOrderType(OrderVO orderVO) {
        Order order = new Order();
        order.setCode(orderVO.getCode());
        order.setAddress(orderVO.getAddress());
        order.setAmount(orderVO.getAmount());
        order.setDeadline(orderVO.getDeadline());
        order.setHandInDate(orderVO.getHandInDate());
        order.setOrderStateForCustomer(orderVO.getCustomerState());
        order.setOrderStateForFactory(orderVO.getFactoryState());
        order.setProductName(orderVO.getProName());
        return order;
    }

    public Bid changeBidType(BidInfoVO bidInfoVO) {
        Bid bid = new Bid();
        bid.setId(Integer.parseInt(bidInfoVO.getId()));
        bid.setBidPrice(bidInfoVO.getPrice());
        bid.setBidState(bidInfoVO.getIsChosen());
        bid.setOrderId(Integer.parseInt(bidInfoVO.getOrderId()));
        bid.setFactoryAccount(userDAO.getById(factoryDAO.findByName(bidInfoVO.getFactory()).getUserId()).getAccount());
        return bid;
    }

    public OrderVO addIdToOrder(Order order) {//将id添加到要返回的VO上
        return new OrderVO(orderDAO.findOrderByCode(order.getCode()));
    }

    //自动生成编号
    public String generateCode() {
        Date date = new Date();
        return "OD" + sdf.format(date);
    }
}
